package com.shengtsource.minicat.model;


import java.io.IOException;
import java.io.InputStream;

/**
 * 请求对象
 * @author shengtsource
 */
public class Request {
    /**
     * URL
     */
    private String url;

    private String host;
    /**
     * 请求方式
     */
    private String method;
    /**
     * 输入流
     */
    private InputStream inputStream;

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }

        //读取数据
        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String requestHeaderStr = new String(bytes);
        String firstLine = requestHeaderStr.split("\\\n")[0];
        String[] headrAttrs = firstLine.split(" ");
        String hostStr = requestHeaderStr.split("\\\n")[1];
        String host = hostStr.split("\\:")[1].trim();
        this.url = headrAttrs[1];
        this.method = headrAttrs[0];
        this.host = host;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
